package pers.huangyuhui.sms.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.huangyuhui.sms.model.Paging;
import pers.huangyuhui.sms.model.TeacherInfo;
import pers.huangyuhui.sms.service.TeacherService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("teacherController")
@ResponseBody
public class TeacherController {

    // 用类型注入的方式获取TeacherService
    @Resource(type = TeacherService.class)
    private TeacherService teacherService;

    /**
     * @param currentPage 当前页
     * @param pageSize 页长
     * @param name 教师姓名
     * @param classID 班级{@code id}
     * @param httpSession {@code session}作用域
     * @return 包含教师列表和列表长度的 map 集合,否则返回null
     */
    @PostMapping("/getTeacherList.do")
    private Map<String , Object> getTeacherList(
            @RequestParam(value = "page" , required = false , defaultValue = "1") Integer currentPage,
            @RequestParam(value = "rows" , required = false , defaultValue = "999") Integer pageSize,
            @RequestParam(value = "teacherName" , required = false , defaultValue = "") String name,
            @RequestParam(value = "clazzid" , required = false , defaultValue = "0") Integer classID,
            HttpSession httpSession){

        //获取用户权限
        Integer userType = (Integer) httpSession.getAttribute("userType");
//        System.out.println(userType);

        // 存储查询条件
        TeacherInfo teacherInfo = new TeacherInfo();
        teacherInfo.setId(0);
        teacherInfo.setName(name);
        teacherInfo.setClassID(classID);

        // 用户权限设置: 如果当前用户类型为教师,则将其权限设置为仅能查询个人信息
        if (userType == 3) {
            TeacherInfo currentTeacherInfo = (TeacherInfo) httpSession.getAttribute("userInfo");// 获取当前登录的教师信息
            teacherInfo.setId(currentTeacherInfo.getId());
        }

        // 将需要的参数全部放进 map 里面
        Paging paging = new Paging(currentPage, pageSize);
        Map<String , Object> map = new HashMap<>();
        map.put("name" , teacherInfo.getName());
        map.put("classID" , teacherInfo.getClassID());
        map.put("id" , teacherInfo.getId());
        map.put("start" , paging.getPageStart());
        map.put("size" , paging.getPageSize());

        try {
            // 获取分页后的教师信息
            List<TeacherInfo> teacherList = teacherService.getTeacherList(map);

            // 获取教师信息列表的条数
            int total = teacherService.getTeacherListNum(teacherInfo);

            // 清空上面的集合,并把查到的数据塞进去
            map.clear();
            map.put("total" , total);
            map.put("rows" , teacherList);

            // 返回结果
            return map;
        } catch (Exception e) {
            // 抛出异常的话就返回 null
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @param teacherInfo 即将被添加的教师信息
     * @param phone 电话
     * @param classID 班级{@code id}
     * @return 成功返回success,失败返回failure,出错返回error
     */
    @PostMapping("/addTeacher.do")
    private String addTeacher(TeacherInfo teacherInfo,
                              @RequestParam(value = "phone" , required = false , defaultValue = "") String phone,
                              @RequestParam(value = "clazzid" , required = false , defaultValue = "0") Integer classID){
        // 前端电话是 phone ,无法直接绑定数据……QAQ
        teacherInfo.setMobile(phone);
        // 前端班级是 clazzid ,id是tm的小写!!!QAQ 吐血!!!
        // ✓ （╯' - ')╯︵ ┻━┻
        teacherInfo.setClassID(classID);

        try {
            if (teacherService.addTeacher(teacherInfo)) {
                return "success";
            } else {
                return "failure";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    /**
     * @param teacherInfo 即将被修改的教师信息
     * @param phone 电话
     * @param classID 班级{@code id}
     * @return 成功返回success,失败返回failure,出错返回error
     */
    @PostMapping("editTeacher.do")
    private String editTeacher(TeacherInfo teacherInfo,
                               @RequestParam(value = "phone" , required = false , defaultValue = "") String phone,
                               @RequestParam(value = "clazzid" , required = false , defaultValue = "0") Integer classID){
        // 同 addTeacher 的原因……
        teacherInfo.setMobile(phone);
        teacherInfo.setClassID(classID);

        try {
            if (teacherService.editTeacherInfo(teacherInfo)){
                return "success";
            } else {
                return "failure";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }

    /**
     * @param ids 由将要被删除的学生的{@code id}组成的数组
     * @return 成功返回success,失败返回failure,出错返回error
     */
    @PostMapping("deleteTeacher.do")
    private String deleteTeacher(@RequestParam(value = "ids[]") String[] ids){
        try {
            if (teacherService.deleteTeacher(ids)) {
                return "success";
            } else {
                return "failure";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }
}
